package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.BargainGroupService;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.ordersync.service.SyncVerifyService;
import com.ymatou.iapi.trading.sellerquery.parameter.GetSellerPaidSalesVolumeBean;
import com.ymatou.iapi.trading.sellerquery.parameter.resp.GetSellerPaidSalesVolumeResp;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 获取商家成交额成交量接口, 新接口, .net没有这个接口 FreeMaker Template自动生成代码
 */
@SuppressWarnings("rawtypes")
@TestSuite
public class Ts_GetSellerPaidSalesVolume {
	private static GetSellerPaidSalesVolumeBean request;
	// private static
	// com.ymatou.iapi.trading.sellerquery.service.GetSellerPaidSalesVolumeCall
	// call;
	private static com.ymatou.iapi.trading.sellerquery2.service.GetSellerPaidSalesVolumeCall call;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商家成交额成交量接口");
	}

	@Before
	public void caseUp() {
		request = new GetSellerPaidSalesVolumeBean();
		// call = new
		// com.ymatou.iapi.trading.sellerquery.service.GetSellerPaidSalesVolumeCall();
		call = new com.ymatou.iapi.trading.sellerquery2.service.GetSellerPaidSalesVolumeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetSellerPaidSalesVolumeResp expectResponse = new GetSellerPaidSalesVolumeResp();
		ConstructExpectedResponse(expectResponse);
		GetSellerPaidSalesVolumeResp actualResponse = call
				.getSellerSalesVolumeResp();
		DataUtil.verify(expectResponse.getSales(), actualResponse.getSales(),
				"验证sales");
		DataUtil.verify(expectResponse.getVolume(), actualResponse.getVolume(),
				"验证volume");
	}

	public void ConstructExpectedResponse(GetSellerPaidSalesVolumeResp response) {
		List<Map> list = execute(request.getSellerId(), request.getBeginTime(),
				request.getEndTime());
		if (false == DataUtil.isNullOrEmpty(list)) {
			DataUtil.mapToBean(response, list.get(0));
		}
	}

	public List<Map> execute(int sellerId, String beginTime, String endTime) {
		return new DefaultWapper().executeSql(getSellerPaidSalesVolumeSql(
				sellerId, beginTime, endTime));
	}

	public static String getSellerPaidSalesVolumeSql(int sellerId,
			String beginTime, String endTime) {
		return " SELECT COUNT(o.iOrderId) AS 'volume',"
				//ISNULL ( check_expression , replacement_value )
				+ " SUM(o.fTotalPrice + o.fFreight + o.fOrderDiscount + o.fDiscount - isnull(o.fUseGiftAmount, 0)"
				+ "     - isnull(o.[fSellerPromotionAmount], 0)- isnull(o.fSellerCouponAmount, 0)-isnull(s.fRefundedAmountOfCash, 0)-isnull(s.fRefundedAmountOfCoupon, 0)) AS 'sales'"
				+ " FROM dbo.Ymt_Orders o WITH ( NOLOCK )"
				//砍价团订单，在组团成功前，结果不展示。add by sean 03.01
				+ "LEFT JOIN dbo.Ymt_OrderBiz b ON b.OrderId = o.iOrderId"
				+ " JOIN dbo.Ymt_OrderState s WITH ( NOLOCK )"
				+ " ON o.iorderid = s.iorderid"
				+ " WHERE o.iBuyerId = "
				+ sellerId
				+ "AND ( b.OrderId IS NULL  OR b.BizStatus =3) "
				+ " AND o.dPaidTime BETWEEN '"
				+ beginTime
				+ "' AND '"
				+ endTime
				+ "' AND o.iTradingStatus IN (2,3,4,16,17) AND s.fPaidAmountOfCash > 0";
	}

	/*	public static String getSellerPaidSalesVolumeSql(int sellerId,
				String beginTime, String endTime) {
			return " SELECT COUNT(o.iOrderId) AS 'volume', SUM(o.fPayableAmount + isnull(o.fYmtCouponAmount, 0)) AS 'sales'"
					+ " FROM dbo.Ymt_Orders o WITH ( NOLOCK )"
					+ " WHERE o.iBuyerId = "
					+ sellerId
					+ " AND o.dPaidTime BETWEEN '"
					+ beginTime
					+ "' AND '"
					+ endTime + "' AND o.iTradingStatus IN (2,3,4,16,17) ";
		}
	*/
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerPaidSalesVolume_001() {
		Logger.start(true, "获取商家成交额成交量");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			request.setSellerId(sellerId);
			request.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-30,
					YMTDateUtil.YMDTHMS_STRING));
			request.setEndTime(YMTDateUtil.getBeforeOrNextDay(-20,
					YMTDateUtil.YMDTHMS_STRING));
			call.setData(request);
			call.callService();
			DataUtil.verify(200, call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerPaidSalesVolume_002() {
		Logger.start(true, "商家id不存在");
		try {

			request.setSellerId(404);
			request.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-50,
					YMTDateUtil.YMDTHMS_STRING));
			request.setEndTime(YMTDateUtil.getBeforeOrNextDay(-20,
					YMTDateUtil.YMDTHMS_STRING));
			call.setData(request);
			call.callService();
			Logger.verifyEquals(ECode.SUCCESS, call.getString("code"), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerPaidSalesVolume_003() {
		Logger.start(true, "商家id为null");
		try {
			request.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-50,
					YMTDateUtil.YMDTHMS_STRING));
			request.setEndTime(YMTDateUtil.getBeforeOrNextDay(-20,
					YMTDateUtil.YMDTHMS_STRING));
			call.setData(request);
			call.callService();
			Logger.verifyEquals(ECode.ERROR400, call.getString("code"),
					"验证Code");
			Logger.verifyEquals("100", call.getString("bcode"), "验证Code");
			Logger.verifyEquals("sellerId:must be greater than or equal to 1",
					call.getString("msg"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetSellerPaidSalesVolume_004() {
		Logger.start(true, "beginTime = null, 获取商家成交额成交量失败");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			request.setSellerId(sellerId);
			request.setEndTime(YMTDateUtil.getBeforeOrNextDay(-20,
					YMTDateUtil.YMDTHMS_STRING));
			call.setData(request);
			call.callService();
			DataUtil.verify(ECode.ERROR400, call.getCode(), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetSellerPaidSalesVolume_005() {
		Logger.start(true, "endTime = null, 获取商家成交额成交量失败");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			request.setSellerId(sellerId);
			request.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-50,
					YMTDateUtil.YMDTHMS_STRING));
			call.setData(request);
			call.callService();
			DataUtil.verify(ECode.ERROR400, call.getCode(), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerPaidSalesVolume_006() {
		Logger.start(true, "砍价团订单,组团成功,获取商家成交额成交量");
		try {
			PlaceOrderResp placeOrderResp = BargainGroupService.openGroup(null);

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			// 砍价团id
			String bizId = placeOrderResp.getBizId();
			// 开团订单
			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 砍价团参团订单
			PlaceOrderResp placeOrderResp2 = BargainGroupService
					.joinGroup(bizId);
			// 参团
			Integer orderId2 = placeOrderResp2.getOrderList().get(0)
					.getOrderId();

			BargainGroupService.notifyOrderBizStatus(bizId, orderId, 1, 3);

			SyncVerifyService.getSyncResult(orderId);

			request.setSellerId(sellerId);
			request.setBeginTime(YMTDateUtil.getBeforeOrNextDay(0,
					YMTDateUtil.YMDTHMS_STRING));
			request.setEndTime(YMTDateUtil.getBeforeOrNextDay(1,
					YMTDateUtil.YMDTHMS_STRING));
			call.setData(request);
			call.callService();
			DataUtil.verify(200, call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerPaidSalesVolume_007() {
		Logger.start(true, "砍价团订单,组团失败,获取商家成交额成交量");
		try {
			PlaceOrderResp placeOrderResp = BargainGroupService.openGroup(null);

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			// 砍价团id
			String bizId = placeOrderResp.getBizId();
			// 开团订单
			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 砍价团参团订单
			PlaceOrderResp placeOrderResp2 = BargainGroupService
					.joinGroup(bizId);
			// 参团
			Integer orderId2 = placeOrderResp2.getOrderList().get(0)
					.getOrderId();

			BargainGroupService.notifyOrderBizStatus(bizId, orderId, 1, -3);

			SyncVerifyService.getSyncResult(orderId);

			request.setSellerId(sellerId);
			request.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-1,
					YMTDateUtil.YMDTHMS_STRING));
			request.setEndTime(YMTDateUtil.getBeforeOrNextDay(1,
					YMTDateUtil.YMDTHMS_STRING));
			call.setData(request);
			call.callService();
			DataUtil.verify(200, call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerPaidSalesVolume_008() {
		Logger.start(true, "砍价团订单,组团成功前,获取商家成交额成交量");
		try {
			PlaceOrderResp placeOrderResp = BargainGroupService.openGroup(null);

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			// 砍价团id
			String bizId = placeOrderResp.getBizId();
			// 开团订单
			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			// 砍价团参团订单
			PlaceOrderResp placeOrderResp2 = BargainGroupService
					.joinGroup(bizId);
			// 参团
			Integer orderId2 = placeOrderResp2.getOrderList().get(0)
					.getOrderId();

			BargainGroupService.notifyOrderBizStatus(bizId, orderId, 1, 2);

			SyncVerifyService.getSyncResult(orderId);

			request.setSellerId(sellerId);
			request.setBeginTime(YMTDateUtil.getBeforeOrNextDay(-1,
					YMTDateUtil.YMDTHMS_STRING));
			request.setEndTime(YMTDateUtil.getBeforeOrNextDay(1,
					YMTDateUtil.YMDTHMS_STRING));
			call.setData(request);
			call.callService();
			DataUtil.verify(200, call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}